import { InputTypeEnum } from "./Enum";
import Singleton from "./Singleton";
import { IBullet, IClientInput, IState } from "./State";

export default class DataManager extends Singleton {
    static get Instance() {
      return super.GetInstance<DataManager>();
    }

    private precision = 3;
    myState = []


    state: IState = {
        actors: [
          {
            id: 2,
            nickname: "哈哈1",
            position: {
              x: -200,
              y: -200,
            },
            direction: {
              x: 1,
              y: 0,
            },
            hp: 100,
          },
          {
            id: 1,
            nickname: "哈哈2",
            position: {
              x: 200,
              y: 200,
            },
            direction: {
              x: 0,
              y: -1,
            },
            hp: 100,
          },
        ],
        bullets: [],
        nextBulletId: 1,
    };

    applyInput(input: IClientInput) {
        switch (input.type) {
            case InputTypeEnum.ActorMove: {
                const {
                    direction: { x, y },
                    dt,
                    angle,
                    id,
                } = input;
                const player = this.state.actors.find((e) => e.id === id);
                if (!player) {
                    return;
                }
      
            //   player.position.x += toFixed(x * PLAYER_SPEED * dt);
            //   player.position.y += toFixed(y * PLAYER_SPEED * dt);
      
            //   player.position.x = clamp(player.position.x, -mapW / 2, mapW / 2);
            //   player.position.y = clamp(player.position.y, -mapH / 2, mapH / 2);
      
                player.direction = { x, y };
                break;
            }
            case InputTypeEnum.WeaponShoot: {
              const { owner, position, direction } = input;
              const bullet: IBullet = {
                id: this.state.nextBulletId++,
                owner,
                position,
                direction,
              };
              this.state.bullets.push(bullet);
      
              break;
            }
            case InputTypeEnum.TimePast: {
              const { dt } = input;
              const { bullets, actors } = this.state;
      
                // for (let i = bullets.length - 1; i >= 0; i--) {
                //     const bullet = bullets[i];
                //     for (let j = actors.length - 1; j >= 0; j--) {
                //     const player = actors[j];
                //     if ((player.position.x - bullet.position.x) ** 2 + (player.position.y - bullet.position.y) ** 2 < (PLAYER_RADIUS + BULLET_RADIUS) ** 2) {
                //         EventManager.Instance.emit(EventEnum.ExplosionBorn, bullet.id, {
                //         x: toFixed((player.position.x + bullet.position.x) / 2),
                //         y: toFixed((player.position.y + bullet.position.y) / 2),
                //         });
        
                //         player.hp -= WEAPON_DAMAGE;
                //         bullets.splice(i, 1);
                //         break;
                //     }
                //     }
                //     if (Math.abs(bullet.position.x) > mapW / 2 || Math.abs(bullet.position.y) > mapH / 2) {
                //     EventManager.Instance.emit(EventEnum.ExplosionBorn, bullet.id, {
                //         x: bullet.position.x,
                //         y: bullet.position.y,
                //     });
                //     bullets.splice(i, 1);
                //     }
                // }
      
                // for (const bullet of this.state.bullets) {
                //     bullet.position.x += toFixed(bullet.direction.x * BULLET_SPEED * dt);
                //     bullet.position.y += toFixed(bullet.direction.y * BULLET_SPEED * dt);
                // }
            }
        }
    }

    fixedPoint(value) {
      // 将浮点数转换为整数，并记录小数点位置
      return Math.round(value * Math.pow(10, this.precision));
    }

    toString(rawValue) {
        return rawValue / Math.pow(10, this.precision);
    }
}